home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Magazine / C_Tutorial / Part-2 / mouse4.c < prev    next >
C/C++ Source or Header  |  1997-07-24  |  4KB  |  155 lines

  1. #include<exec/libraries.h>
  2. #include<intuition/intuition.h>
  3. #include<utility/tagitem.h>
  4. #include<graphics/text.h>
  5. #include<graphics/rastport.h>
  6.  
  7. #include<string.h>
  8.  
  9. #include<clib/exec_protos.h>
  10. #include<clib/graphics_protos.h>
  11. #include<clib/intuition_protos.h>
  12. #include<clib/layers_protos.h>
  13.  
  14. struct Library* GfxBase;
  15. struct Library* IntuitionBase;
  16. struct Library* LayersBase;
  17.  
  18. /* Need to give prototypes for our functions */
  19. void handleIDCMP(struct Window*);
  20. int setClip(struct Window*);
  21. void removeClip(struct Window*);
  22.  
  23. void main()
  24. {
  25.     /* Open libraries... */
  26.     if(GfxBase = OpenLibrary("graphics.library",36))
  27.     {
  28.         if(IntuitionBase = OpenLibrary("intuition.library",36))
  29.         {
  30.             if(LayersBase = OpenLibrary("layers.library",36))
  31.             {
  32.                 /* Open our window */
  33.                 struct Window* win;
  34.                 if(win = OpenWindowTags(NULL,
  35.                                                                 WA_Left,        20,
  36.                                                                 WA_Top,            20,
  37.                                                                 WA_Width,        200,
  38.                                                                 WA_Height,    100,
  39.                                                                 WA_Flags,        WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_REPORTMOUSE,
  40.                                                                 WA_IDCMP,        IDCMP_CLOSEWINDOW | IDCMP_MOUSEBUTTONS | IDCMP_MOUSEMOVE,
  41.                                                                 TAG_DONE,        0))
  42.                 {
  43.                     /* If window opened, set clip region */
  44.                     if(setClip(win))
  45.                     {
  46.                         /* Now handle messages */
  47.                         handleIDCMP(win);
  48.                         removeClip(win);
  49.                     }
  50.                     CloseWindow(win);
  51.                 }
  52.                 CloseLibrary(LayersBase);
  53.             }
  54.             CloseLibrary(IntuitionBase);
  55.         }
  56.         CloseLibrary(GfxBase);
  57.     }
  58. }
  59.  
  60. /* Our message handling code */
  61. void handleIDCMP(struct Window* win)
  62. {
  63.     char* text = "Hello World!";
  64.     int going = TRUE;
  65.     int drawing = FALSE;
  66.     UBYTE pen = 1;
  67.     SetDrMd(win->RPort, JAM1);
  68.     while(going)
  69.     {
  70.         struct IntuiMessage* intuimsg;
  71.         /* Wait for messages to arrive */
  72.         WaitPort(win->UserPort);
  73.         /* Messages have arrived: loop through all of them */
  74.         while(intuimsg = (struct IntuiMessage*)GetMsg(win->UserPort))
  75.         {
  76.             /* Act on this message... */
  77.             switch(intuimsg->Class)
  78.             {
  79.             case IDCMP_MOUSEBUTTONS:
  80.                 switch(intuimsg->Code)
  81.                 {
  82.                 case SELECTDOWN:
  83.                     drawing = TRUE;
  84.                     break;
  85.                 case SELECTUP:
  86.                     drawing = FALSE;
  87.                     break;
  88.                 }
  89.                 /* Omit the break to draw on click, too */
  90.                 break;
  91.             case IDCMP_MOUSEMOVE:
  92.                 if(drawing)
  93.                 {
  94.                     /* Try changing colour using something like this: */
  95.                     SetAPen(win->RPort, pen++);
  96.                     /* Or this: */
  97.                     /* SetAPen(win->RPort, (UBYTE)(intuimsg->MouseY / 8)); */
  98.                     /* Or something of your own invention... */
  99.                     Move(win->RPort, intuimsg->MouseX, intuimsg->MouseY);
  100.                     Text(win->RPort, text, strlen(text));
  101.                 }
  102.                 break;
  103.             case IDCMP_CLOSEWINDOW:
  104.                 going = FALSE;
  105.                 break;
  106.             }
  107.             /* Reply when finished with message */
  108.             ReplyMsg((struct Message*)intuimsg);
  109.         }
  110.     }
  111. }
  112.  
  113. /* Set a clip region on internal part of window */
  114. int setClip(struct Window* win)
  115. {
  116.     /* Make a new region */
  117.     struct Region* reg;
  118.     if(reg = NewRegion())
  119.     {
  120.         /* Make a rectangle that describes the inside of the window */
  121.         struct Rectangle rect;
  122.         rect.MinX = win->BorderLeft;
  123.         rect.MinY = win->BorderTop;
  124.         rect.MaxX = win->Width - win->BorderRight - 1;
  125.         rect.MaxY = win->Height - win->BorderBottom - 1;
  126.         /* Make the region equal to this rectangle */
  127.         if(OrRectRegion(reg, &rect))
  128.         {
  129.             /* Set the clip region on the window's layer */
  130.             InstallClipRegion(win->WLayer, reg);
  131.             /* Say we succeeded */
  132.             return TRUE;
  133.         }
  134.         else
  135.         {
  136.             /* Failed to set region, so delete it */
  137.             DisposeRegion(reg);
  138.         }
  139.     }
  140.     /* If we get this far they we've failed */
  141.     return FALSE;
  142. }
  143.  
  144. /* Remove the clip region from a window */
  145. void removeClip(struct Window* win)
  146. {
  147.     struct Region* reg;
  148.     if(reg = InstallClipRegion(win->WLayer, NULL))
  149.     {
  150.         /* If a clip region is installed it's our new one, so delete it */
  151.         DisposeRegion(reg);
  152.     }
  153. }
  154.  
  155.